const path = require("path"); // node内置核心模块，用来设置路径。
const MiniCssExtractPlugin = require('mini-css-extract-plugin') //把css打包成独立文件
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin"); //压缩css
const HtmlWebpackPlugin = require('html-webpack-plugin'); //打包html
const ESLintPlugin = require('eslint-webpack-plugin') //格式化校验
const CopyWebpackPlugin = require("copy-webpack-plugin") //打包公共文件


module.exports = {
    //打包模式
    mode: "production", //还有development模式
    //入口文件
    // entry: "./src/index.js",
    entry: path.resolve(__dirname,"../src/index.js"),
    //出口文件
    output: {
        path: path.resolve(__dirname, "../build"),
        filename: "./js/bundle.js"
    },


    //模块配置，配置loader
    module: {
        rules: [{
            test: /\.css$/i,
            use: [
                //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                MiniCssExtractPlugin.loader,
                'css-loader'
            ]
        }, {
            test: /\.less$/i,
            use: [
                //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                MiniCssExtractPlugin.loader,
                'css-loader',
                'less-loader'
            ]
        },{
            test: /\.(png|jpg|gif|svg)$/,
            //asset 在导出一个 data URI 和发送一个单独的文件之间自动选择。代替之前的url-loader和file-loader
            type: "asset",
            parser: {
                dataUrlCondition: {
                    maxSize: 30 * 1024, // 小于15kb以下的图片会被打包成base64格式
                },
            },
            generator: {
                filename: './images/[name][ext]'
            }
        },]
    },


    //插件配置
    plugins: [new MiniCssExtractPlugin({
        //输出独立文件的文件名
        filename: './css/main.css'
    }), new HtmlWebpackPlugin({
        template: path.resolve(__dirname, "../public/index.html"),
        filename: "./index.html",
        minify: {
            collapseWhitespace: true,
            keepClosingSlash: true,
            removeComments: true,
            removeRedundantAttributes: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true,
            useShortDoctype: true
        },
    }), new ESLintPlugin({
        fix: true
    }),new CopyWebpackPlugin({
        patterns: [{
            from: path.resolve(__dirname, "../public"),
            to: "public",
            //配置复制时忽略的文件
            globOptions: {
                ignore: ["**/index.html"]
            }
        }],
    }),],

    //优化的配置 压缩css
    optimization: {
        minimizer: [
            new CssMinimizerPlugin(),
        ],
    },
}